﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace Cryto_Provider
{
    public class Hash_Provider
    {
        private static RandomNumberGenerator rand = new RNGCryptoServiceProvider();
        
        public static byte[] GenerateRandomBytes(int count)
        {
            byte[] bytes = new byte[count];
            rand.GetBytes(bytes);
            return bytes;
        }

        public byte[] XOR(byte[] bytepass, byte[] salt)
        {
            if (bytepass.Length >= salt.Length)
            {
                byte[] xorbyte = new byte[bytepass.Length];
                for (int i = 0; i < salt.Length; i++)
                {
                    xorbyte[i] = (byte)(bytepass[i] ^ salt[i]);
                }
                return xorbyte;
            }
            else
            {
                byte[] xorbyte = new byte[salt.Length];
                for (int i = 0; i < bytepass.Length; i++)
                {
                    xorbyte[i] = (byte)(bytepass[i] ^ salt[i]);
                }
                return xorbyte;
            }
        }
        //private static SymmetricAlgorithm CreateRijndael(string password, byte[] salt)
        //{
        //    PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, salt, "SHA256", 1000);

        //    SymmetricAlgorithm sma = Rijndael.Create();
        //    sma.KeySize = 256;
        //    sma.Key = pdb.GetBytes(32);
        //    sma.Padding = PaddingMode.PKCS7;
        //    return sma;
        //}

        public byte[] Hash_file(string filename)
        {
            //Declarations
            Byte[] s_Bytes;
            Byte[] encodedBytes;

            StreamReader reader = new StreamReader(filename);
            string s = reader.ReadToEnd();
            reader.Close();
            HashAlgorithm md5;

            //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
            md5 = new MD5CryptoServiceProvider();
            s_Bytes = ASCIIEncoding.Default.GetBytes(s);
            encodedBytes = md5.ComputeHash(s_Bytes);

            //Convert encoded bytes back to a 'readable' string
            
            return encodedBytes;
        }

        public byte[] PassHash(string spassword, string salt)
        {
            //Declarations
            Byte[] passBytes;
            Byte[] saltBytes;
            Byte[] encodedBytes;
            MD5 md5;

            //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
            md5 = new MD5CryptoServiceProvider();
            passBytes = ASCIIEncoding.Default.GetBytes(spassword);
            saltBytes = ASCIIEncoding.Default.GetBytes(spassword);
            encodedBytes = md5.ComputeHash(XOR(passBytes,saltBytes));

            //Convert encoded bytes back to a 'readable' string
            //return BitConverter.ToString(encodedBytes);
            return encodedBytes;
        }
    }
}
